package com.travel.ticket.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.travel.ticket.entity.AttrEntity;
import com.travel.ticket.entity.SkuInfoEntity;
import com.travel.ticket.entity.SpuRelationAttr;
import com.travel.ticket.service.AttrService;
import com.travel.ticket.service.SkuInfoService;
import com.travel.ticket.to.AttrTo;
import com.travel.ticket.vo.AttrVo;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.common.utils.PageUtils;
import com.travel.common.utils.Query;

import com.travel.ticket.dao.SpuInfoDao;
import com.travel.ticket.entity.SpuInfoEntity;
import com.travel.ticket.service.SpuInfoService;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    AttrService attrService;

    @Autowired
    SkuInfoService skuInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取spu的所有attr属性
     */
    @Override
    public List<AttrVo> getRelationAttrs(Integer spuId) {
        List<AttrVo> attrVos = new ArrayList<>();
        //1. 获取该spuId与attr的关联实体类
        List<SpuRelationAttr> spuRelationAttrs = attrService.getSpuIdRelationAttr(spuId);
        spuRelationAttrs.stream().forEach(attr->{
            AttrVo attrVo = new AttrVo();
            attrVo.setAttrId(attr.getAttrId());
            List<String> selectValues = Arrays.asList(attr.getSelectValues().split(","));
            attrVo.setSelectValues(selectValues);

            AttrEntity byId = attrService.getById(attr.getAttrId());
            attrVo.setAttrName(byId.getAttrName());

            attrVos.add(attrVo);
        });

        return attrVos;
    }

    @Override
    public void saveSpuRelationAttr(Integer spuId, List<AttrTo> attrTos) {
        for (AttrTo attrTo : attrTos) {
            Integer attrId = attrTo.getAttrId();
            String selectValues = String.join(",", attrTo.getSelectValues());
            this.baseMapper.saveSpuRelationAttr(spuId,attrId,selectValues);
        }
    }

    /**
     * 获取该spu对应所有sku中的最低价格 用于页面展示
     * @param spuId
     * @return
     */
    @Override
    public BigDecimal getMinPrice(Integer spuId) {
        List<SkuInfoEntity> skuListBySpuId = skuInfoService.getSkuListBySpuId(spuId);
        BigDecimal minPrice = skuListBySpuId.stream()
                .map(SkuInfoEntity::getPrice)
                .filter(price -> price != null) // 过滤空值
                .min(Comparator.naturalOrder()) // 使用自然顺序比较器获取最小值
                .orElse(null); // 如果最小值不存在，则返回 null
        return minPrice;
    }

}